package com.e.ego.fragment

import android.app.Activity
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

/**
 * import kotlinx.android.synthetic.main.activity_kotlin.*
 * Created by lxy on 2017/5/28.
 */
abstract class EFragment<A : FragmentActivity> : Fragment() {

    var mActivity: A? = null
    var mContentView: View? = null

    abstract fun layout(): Int
    abstract fun onFragmentCreated(contentView:View,state: Bundle?)

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        if (mContentView == null) {
            var view = inflater.inflate(layout(), null)
            mContentView = view
            onFragmentCreated(view, savedInstanceState)
        } else {
            val parent = mContentView!!.getParent() as ViewGroup
            parent?.removeAllViewsInLayout()
        }
        return mContentView
    }

    override fun onAttach(activity: Activity) {
        super.onAttach(activity)
        mActivity = activity as A
    }

    fun activity():A? = mActivity
}